<template>
  <div class="login_area">
    <div class="login">
      <h3 class="title">叩丁狼商品管理系统</h3>
      <el-form
        ref="ruleFormRef"
        :model="ruleForm"
        status-icon
        :rules="rules"
        class="demo-ruleForm"
      >
        <el-form-item prop="username">
          <el-input v-model="ruleForm.username" placeholder="请输入账号">
            <template #prefix>
              <el-icon :size="20"><User /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="ruleForm.password"
            placeholder="请输入密码"
            type="password"
          >
            <template #prefix>
              <el-icon :size="20"><Lock /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm(ruleFormRef)"
            >登录</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from "vue";
import type { FormInstance } from "element-plus";
import { LoginAPI } from "./api/index";
import { useRouter } from "vue-router";
//icon图标
import { User, Lock } from "@element-plus/icons-vue";
//提示框
import { ElMessage } from "element-plus";
//获取dome元素
const ruleFormRef = ref<FormInstance>();
//获取路由跳转使用
const router = useRouter();

const validatePass = (rule: any, value: string, callback: any) => {
  if (value === "") {
    callback(new Error("请输入密码"));
  } else {
    callback();
  }
};

const ruleForm = reactive({
  username: "",
  password: "",
});

const rules = reactive({
  username: [{ required: true, message: "请输入账号", trigger: "blur" }],
  password: [{ validator: validatePass, trigger: "blur" }],
});

const submitForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.validate(async (valid) => {
    if (valid) {
      //在这里发送登录请求
      let res = await LoginAPI(ruleForm);
      //1.存登录凭证
      localStorage.setItem("token", res.data.tokenHead + res.data.token);
      //2.跳转到首页
      router.push("/index");
      //3.提示
      ElMessage.success("登录成功");
    } else {
      ElMessage.error("请输入账号密码");
      return false;
    }
  });
};

console.log(router.getRoutes());

//局部自定义指令防抖
</script>

<style scoped lang="less">
.login_area {
  width: 100%;
  height: 100%;
  background-color: #2d3a4b;
  overflow: hidden;
  .login {
    width: 420px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);

    text-align: center;
    //输入框
    :deep(.el-input__wrapper) {
      background-color: #2d3a4b;
      height: 48px;
      border: 1px solid #3e4957;
      box-shadow: none;
    }
    //输入框颜色
    :deep(.el-input__inner) {
      color: white;
    }
    //提交按钮
    :deep(.el-button) {
      width: 100%;
      height: 40px;
    }
    //图标
    :deep(.el-icon) {
      // color: #454545;
      color: white;
    }
    .title {
      width: 100%;
      height: 51px;
      line-height: 51px;
      font-size: 24px;
      color: white;
    }
  }
}
</style>
